#include "creator.h"
#include "../../semantics/visitordata.h"
#include "../../semantics/classsemantic.h"
#include "../../semantics/fixclients.h"
#include "../../semantics/error.h"

void CreatorNode::write(GraphStream * s)
{
	this->accept(s);
	fprintf(s->stream,"\tnode_%d [label=\"(%d,%d)\\nCreator node\" shape=\"rectangle\"]\n", this->number(),this->line(),this->position());
	if (m_clients)
	{
	m_clients->write(s);
	fprintf(s->stream,"\tnode_%d -> node_%d [label=\"clients\"]\n",this->number(),m_clients->number());
	}
	m_features->write(s);
	fprintf(s->stream,"\tnode_%d -> node_%d [label=\"features\"]\n",this->number(),m_features->number());
}

void CreatorNode::createConstructors(ParentData * parent)
{
	std::vector<std::string> clients;
	if (m_clients)
	{
		for (unsigned int i=0;i<m_clients->vector().size();i++)
		{
			clients.push_back(m_clients->vector()[i]->data());
		}
		fixClients(clients,parent->CurrentClass->cnode()->name());
	}
	if (m_features)
	{
		for (unsigned int i=0;i<m_features->vector().size();i++)
		{
			std::string name=m_features->vector()[i]->data();
			Method * method=parent->CurrentClass->getMethodByName(name);
			if (method)
			{
				std::vector<std::string> myclients=clients;
				if (myclients.size()==0)
					myclients=method->getClients();
				bool result=parent->CurrentClass->addConstructor(name,myclients);
				if (!result)
					parent->Classes->add(new ConstructorAlreadyDefined(m_features->vector()[i]));
			}
			else parent->Classes->add(new MethodDoesNotExists(m_features->vector()[i]));
		}
	}
}